import platform
import yaml
import logging 

config = None
configFileName='config.txt'

#===============================================================================
# Logging
#===============================================================================
logFilename="debug.log"
logging.basicConfig(filename=logFilename,level=logging.DEBUG)
# Clear log
with open(logFilename, 'w'):
    pass
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)


# Check if program is running on raspberry pi
def isPi():
        if platform.machine()=='armv6l':
            return True
        else:
            return False
        
#===============================================================================
# Load config file
#===============================================================================
def loadConfigFile():
    try: 
        with open(configFileName, 'r') as f:
            config = yaml.load(f)
    except IOError:
        logging.debug("Failed to load config file. Creating new file with default settings.")
        config = createDefaultConfigFile()
    return config
        
#===============================================================================
# Create config file
#===============================================================================
def createDefaultConfigFile():
    with open(configFileName, 'w') as f:
        #config = yaml.load(f)
        #config = {'UI': {'screenHeight': 240, 'screenWidth': 320, 'fontSize': None, 'System': None, 'log': 'debug.log'}, 'Squeezebox': {'playerMAC': None, 'serverIP': 'q', 'serverPort': 9000}}
        config = dict(
                      UI=dict(
                              screenHeight = 240,
                              screenWidth = 320,
                              fontSize = None
                      ),
                      Squeezebox=dict(
                              serverIP='192.168.1.123',
                              serverPort='9000',
                              playerMAC='08:11:96:5f:bc:38',
                              eth0MAC='b8:27:eb:3b:6d:fb',
                              wlan0MAC='74:da:38:00:7d:ec'
                      )                                  
                      )
        f.write('#Configuration file\n\n')
        f.write(yaml.dump(config, default_flow_style=False))
    return config
        

#===============================================================================
# Load configuration file
#===============================================================================
# with open('config.txt', 'r') as f:
#     config = yaml.load(f)
#     
# print config["UI"]["screenWidth"]
# 
# print config["System"]["log"]
# 
# server_url=sb.discover()
# 
# 
# if __name__ == '__main__':
